/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;
import com.globant.google.mendoza.malbec.Order;

/** Represents the mendoza charge order command.
 */
public final class MendozaChargeOrderCommand extends MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaChargeOrderCommand.class);

  /** The amount label. */
  private final String chargeAmountLabel = "Amount";

  /** The order amount to charge. */
  private float amountToCharge = -1;

  /** Creates an instance of MendozaChargeOrderCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaChargeOrderCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
    super(mendozaRequest, mendoza, mendozaState);
    setName("Charge order");
  }

  /** Executes the mendoza server charge order command.
   */
  public void execute() {
    log.trace("Entering execute");
    MendozaCommandResult result = new MendozaCommandResult();
    String commandResultMsg =
        "OK - Charge order command sent to the mendoza server.";
    try {
      MendozaServerState state = this.getMendozaState();
      if (!state.isNewOrderNotificationReceived()
          || !state.isRiskInformationNotificationReceived()) {
        throw new RuntimeException(
            "The order can not be charged in its"
            + " current financial order state.");
      }
      setRequestParameters();
      Order order = this.getMendozaState().getOrder();
      if (amountToCharge < 0) {
        order.charge();
      } else {
        order.charge(new BigDecimal(amountToCharge));
      }
      result.setSuccess(commandResultMsg);
    } catch (RuntimeException e) {
      commandResultMsg = e.getMessage();
      result.setError(commandResultMsg);
    }
    setResult(result);
    logCommand(this);
    log.trace("Leaving execute");
  }

  /** Sets the charge order command parameters.
   */
  private void setRequestParameters() {
    log.trace("Entering setRequestParameters");
    String amountStr =
      getMendozaRequest().getRequestParameters().getProperty("amount");
    if (amountStr == null) {
      logRequestParameter(chargeAmountLabel, "Completely");
    } else {
      try {
        amountToCharge = Float.parseFloat(amountStr);
      } catch (Exception e) {
        log.trace("Leaving setRequestParameters");
        throw new RuntimeException("The amount must be a positive number");
      }
      if (amountToCharge < 0) {
        log.trace("Leaving setRequestParameters");
        throw new RuntimeException("The amount cannot be a negative number");
      }
      logRequestParameter(chargeAmountLabel, amountStr);
    }
    log.trace("Leaving setRequestParameters");
  }
}

